<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
            <a-col :md="6" :sm="8">
                <a-form-item label="分组名称">
                    <a-select v-model="queryParam.fzid" placeholder="请选择分组" @change="handleSelectFz">
                        <a-select-option v-for="item in fsfzlist" :key="item.id" :value="item.id">
                            {{item.wysd_fz_mc}}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
                <a-form-item label="推荐获奖等级">
                    <a-select v-model="queryParam.jxdj" placeholder="请选择奖项等级" @change="handleSelectJxdj">
                        <a-select-option value="0">特等奖</a-select-option>
                        <a-select-option value="1">一等奖</a-select-option>
                        <a-select-option value="2">二等奖</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        <span slot="bili" slot-scope="text, record">
            <span :class="minValueItem.id === record.id ? 'minValEle' : ''">{{ record.bili }}</span>
        </span>
      </a-table>
    </div>

    <cxj-wysd-tp-modal ref="modalForm" @ok="modalFormOk"></cxj-wysd-tp-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CxjWysdTpModal from './modules/CxjWysdTpModal'
  import { filterObj } from '@/utils/util';
  import {
      getAction
  } from "@/api/manage"
  export default {
    name: 'CxjWysdTpList',
    // mixins:[JeecgListMixin, mixinDevice],
    components: {
      CxjWysdTpModal
    },
    data () {
        return {
            description: '委员审定投票表管理页面',
            url: {
                list: "/cxj/cxjWysdTp/cxjwysd_tpjg",
                importExcelUrl: "cxj/cxjWysdTp/importExcel",
                // 获取分组
                cxjwysdFzlist: "/cxj/cxjWysdFz/cxjwysd_fzlist"
            },
            /* 列表参数开始 */
            queryParam: {},
            dictOptions:{},
            superFieldList:[],
            fileStatus: "",
            selectedRowKeys: [],
            selectionRows: [],
            loading: false,
            isorter: {
                column: 'createTime',
                order: 'desc',
            },
            dataSource: [],
            ipagination: {
                current: 1,
                pageSize: 300,
                pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
            /* 列表参数结束 */
            // 表头
            columns: [
                {
                    title: '序号',
                    dataIndex: '',
                    key:'rowIndex',
                    width:60,
                    align:"center",
                    customRender:function (t,r,index) {
                        return parseInt(index)+1;
                    }
                },
                {
                    title:'成果编号',
                    align:"center",
                    dataIndex: 'cxj_xmbh',
                    customCell: () => {
                        return {
                            style: {
                                'color':this.color,
                            },
                        };
                    },
                },
                {
                    title:'成果名称',
                    align:"center",
                    dataIndex: 'cxj_mc_zw'
                },
                {
                    title:'推荐奖项',
                    align:"center",
                    dataIndex: 'xm_hjdj_show'
                },
                {
                    title:'投票结果',
                    align:"center",
                    dataIndex: '',
                    children: [
                        {
                            title: '同意',
                            align: "center",
                            width: '5%',
                            dataIndex: 'tysl',
                            customCell: () => {
                                return {
                                    style: {
                                        'min-width': '100px',
                                    },
                                };
                            },
                        },{
                            title: '不同意',
                            align: "center",
                            width: '5%',
                            dataIndex: 'btysl',
                            customCell: () => {
                                return {
                                    style: {
                                        'min-width': '100px',
                                    },
                                };
                            },
                        },
                        {
                            title: '弃权',
                            align: "center",
                            width: '6%',
                            dataIndex: 'qqsl',
                            customCell: () => {
                                return {
                                    style: {
                                        'min-width': '100px',
                                    },
                                };
                            },
                        },
                    ]
                },
                {
                    title:'同意票数所占比例',
                    align:"center",
                    // dataIndex: 'bili',
                    scopedSlots: { customRender: 'bili' }
                },
                {
                    title:'结果',
                    align:"center",
                    dataIndex: 'sftg'
                },
                // {
                //     title: '操作',
                //     dataIndex: 'action',
                //     align:"center",
                //     fixed:"right",
                //     width:147,
                //     scopedSlots: { customRender: 'action' }
                // }
            ],
            dictOptions:{},
            superFieldList:[],
            // 分组参数
            fsfzlist: [],
            polling: null,
            color: "#000",
            minValueItem: null
        }
    },
    created() {
        this.getSuperFieldList();
        this.getFzList();
        this.pollData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },

    beforeDestroy(){
        clearInterval(this.polling);
    },
    methods: {

        pollData(){
            this.polling = setInterval(()=>{
                this.loadData();
            },3000)
        },
        getFzList(){
            getAction(this.url.cxjwysdFzlist).then((res) => {
                if (res.success) {
                    this.fsfzlist = res.result.records || res.result;
                    this.queryParam.fzid = this.fsfzlist[0].id;
                    this.queryParam.jxdj = "0";
                    this.loadData();
                }
          })
        },
        handleSelectJxdj(value){
            this.queryParam.jxdj = value;
            this.loadData();
        },
        handleSelectFz(value){
            this.queryParam.fzid = value;
            this.loadData();
        },
        /* 获取列表固定格式开始 */
        getQueryField() {
            //TODO 字段权限控制
            var str = "id,";
            this.columns.forEach(function (value) {
                str += "," + value.dataIndex;
            });
            return str;
        },
        // 获取分组项目
        loadData(arg) {
            if (!this.url.list) {
                this.$message.error("请设置url.list属性!")
                return
            }
            //加载数据 若传入参数1则加载第一页的内容
            if (arg === 1) {
                this.ipagination.current = 1;
            }
            var params = this.getQueryParams();//查询条件
            this.loading = true;
            // 分组id
            getAction(this.url.list, params).then((res) => {
                if (res.success) {
                    //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                    this.dataSource = res.result.tpjgList || res.result;
                    this.isTp = res.result.isTp;
                    this.dataSource.map(item=>{
                        // item.tp_jg = "1"
                        if (!item.tp_jg){
                            item.tp_jg = "1"
                        }
                    })
                    if (this.dataSource.length) {
                        this.minValueItem = this.dataSource.reduce((prev, current) => (prev.bili < current.bili ? prev : current));
                        console.log(this.minValueItem, this.minValueItem.bili);
                        if (this.dataSource[0].qqsl+this.dataSource[0].btysl+this.dataSource[0].tysl == res.result.zjsl) {
                            clearInterval(this.polling);
                            this.color = "green";
                        } else {
                            this.color = "#000";
                            clearInterval(this.polling);
                            this.polling = setInterval(()=>{
                                this.loadData();
                            },5000)
                        }
                    } else {
                        clearInterval(this.polling);
                        this.polling = setInterval(()=>{
                            this.loadData();
                        },5000)
                    }
                    if (res.result.total) {
                        this.ipagination.total = res.result.total;
                    } else {
                        this.ipagination.total = 0;
                    }
                    //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                }
                if (res.code === 510) {
                    this.$message.warning(res.message)
                }
                this.loading = false;
            })
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        onClearSelected() {
            this.selectedRowKeys = [];
            this.selectionRows = [];
        },
        modalFormOk() {
            // 新增/修改 成功时，重载列表
            this.loadData();
            //清空列表选中
            this.onClearSelected()
        },
        handleTableChange(pagination, filters, sorter) {
            //分页、排序、筛选变化时触发
            //TODO 筛选
            if (Object.keys(sorter).length > 0) {
                this.isorter.column = sorter.field;
                this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
            }
            this.ipagination = pagination;
            this.loadData();
        },

        getQueryParams() {
            //获取查询条件
            let sqp = {}
            if (this.superQueryParams) {
                sqp['superQueryParams'] = encodeURI(this.superQueryParams)
                sqp['superQueryMatchType'] = this.superQueryMatchType
            }
            this.queryParam.shijian = this.shijian;
            var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters);
            param.field = this.getQueryField();
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return filterObj(param);
        },
        searchQuery() {
            this.loadData(1);
        },
        searchReset() {
            this.queryParam = {};
            // this.flid = "";
            this.shijian = moment(new Date()).format('YYYY');
            this.queryParam.shijian = this.shijian;
            this.loadData(1);
        },
        /* 获取列表固定格式结束 */
        initDictConfig(){
        },
        getSuperFieldList(){
            let fieldList=[];
            fieldList.push({type:'string',value:'cxjCxjId',text:'成果ID',dictCode:''})
            fieldList.push({type:'string',value:'sysZjId',text:'专家ID',dictCode:''})
            fieldList.push({type:'string',value:'xmHjdj',text:'获奖等级',dictCode:''})
            fieldList.push({type:'string',value:'xmHjdjShow',text:'显示获奖等级',dictCode:''})
            fieldList.push({type:'string',value:'tpJg',text:'投票结果',dictCode:''})
            fieldList.push({type:'string',value:'tpZt',text:'投票状态',dictCode:''})
            fieldList.push({type:'string',value:'isValid',text:'是否有效',dictCode:''})
            fieldList.push({type:'string',value:'shijian',text:'时间',dictCode:''})
            this.superFieldList = fieldList
        }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .minValEle {
    color: red;
    font-weight: bolder;
  }
</style>